package store.lunangangster.bpmn.converter.parser;

import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLStreamReader;
import store.lunangangster.bpmn.constant.BpmnXmlConstant;
import store.lunangangster.bpmn.converter.Targetable;
import store.lunangangster.bpmn.model.BaseElement;
import store.lunangangster.bpmn.model.BpmnModel;
import store.lunangangster.bpmn.util.BpmnXmlUtil;

public abstract class AbstractBpmnElementParser implements Targetable, BpmnXmlConstant {

  // 用于子元素解析的解析器集合
  protected Map<String, BaseChildElementParser> childElementParsers = new HashMap<>();

  protected void addChildElementParser(BaseChildElementParser childElementParser) {
    childElementParsers.put(childElementParser.getTargetName(), childElementParser);
  }

  /**
   * 解析出父元素中合法的子元素，并且装载到父元素中
   * @param xsr            xml输入流
   * @param parentElement  父元素对象
   * @param parentName     父元素名
   * @param model          当前BpmnModel对象
   */
  protected void parseChildElement(XMLStreamReader xsr, BaseElement parentElement, String parentName, BpmnModel model)
      throws Exception {

    BpmnXmlUtil.parseChildElement(xsr, parentElement, parentName, model, new HashMap<>(childElementParsers));
  }
}
